package com.whately.core.base.constant;

import lombok.Getter;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import javax.servlet.http.HttpServletRequest;

@Component
@Slf4j
public class DebugSwitcher {

    @Getter
    @Value("${spring.profiles.active:default}")
    private String profilesActive;

    //静态变量：profile是否是local或者dev
    private static boolean isLocalOrDevProfileFlag=false;

    private static String currentProfileActive ;

    @PostConstruct
    public void init(){
        isLocalOrDevProfileFlag = StringUtils.equals(profilesActive,"local")||StringUtils.equals(profilesActive,"dev");
        currentProfileActive = profilesActive;
        log.info("debug switcher profilesActive {} , currentProfileActive {} ,isDebugEnable {} ",profilesActive,currentProfileActive,isLocalOrDevProfileFlag);
    }

    public static String getCurrentProfileActive(){
        return currentProfileActive;
    }


    public static boolean isDebugOpen(HttpServletRequest request){
        ////是否启用,只有测试环境才能启用,而且要求参数必须传参
        if(isLocalOrDevProfileFlag){
            if("XYZ".equals(request.getParameter("debug"))){
                return true;
            }
        }
        return false;
    }


    public static boolean isProfileLocalOrDev(){
        return isLocalOrDevProfileFlag;
    }


}
